package ibm.cognos.imt


import ibm.cognos.imt.dataUsage.DataItemWrapper
import ibm.cognos.imt.dataUsage.*

/**
 * Created with IntelliJ IDEA.
 * User: eric
 * Date: 12/30/13
 * Time: 1:30 PM
 * To change this template use File | Settings | File Templates.
 */
class DataItem implements DataItemWrapper{
    public static final int INTEGER = 1
    public static final int DECIMAL = 2
    public static final int STRING = 3

    def final exp
    def aggregate = "none"
    def name
    def dataUsage = "attribute"
    def dataType
    def owner


    def DataItem getDataItem() {
        this
    }

    DataItem(exp, report, closure = null) {
        this.exp = exp
        name = exp.substring(exp.lastIndexOf("[") + 1, exp.lastIndexOf("]"))
        owner = report
        if (closure) {
            closure.call()
        }
    }

    def dataType(code) {
        dataType = code
        if (owner.dataItemList.size() > 0) {
            for (dataItem in owner.dataItemList) {
                if (name == dataItem.name) {
                    owner.dataItemList.remove(dataItem)
                    owner.dataItemList.add(this)
                }
            }
        }
        this
    }

    def refName(name) {
        this.name = name
        this
    }

    def DataItemWrapper like(Class clazz){
       asType(clazz)
    }


    def DataItemWrapper asType(Class clazz) {
//        if (clazz == Fact) {
//
//            ibm.cognos.imt.dataUsage = "fact"
//            aggregate = "total"
//            return new Fact(this)
//        }
        if (clazz == Slices) {
            dataUsage = "attribute"
            return new Slices(this)
        }
        if (clazz == Measure) {
            dataUsage = "total"
            return new Measure(this)
        }
        if (clazz == Categories) {
            return new Categories(this)
        }
        if (clazz == CrosstabColumn) {
            return new CrosstabColumn(this)
        }
        if (clazz == CrosstabRow) {
            return new CrosstabRow(this)
        }
        if (clazz == XAxisMeasure) {
            return new XAxisMeasure(this)
        }
        if (clazz == YAxisMeasure) {
            return new YAxisMeasure(this)
        }
        if (clazz == BubbleMeasure) {
            return new BubbleMeasure(this)
        }
        if (clazz == Series) {
            return new Series(this)
        }
        else {


            return super.asType(clazz)
        }
    }

    def generateSpec(xmlBuilder) {
        xmlBuilder.dataItem(aggregate: "$aggregate", name: "$name") {
            expression(exp)
            XMLAttributes {
                XMLAttribute(output: "no", name: "RS_dataType", value: "$dataType")
                XMLAttribute(output: "no", name: "RS_dataUsage", value: "$dataUsage")
            }

        }
    }
}

//class Attribute {}

//<dataItem aggregate="total" name="Quantity">
//    <expression>[Sales (query)].[Sales].[Quantity]</expression>
//    <XMLAttributes>
//        <XMLAttribute output="no" name="RS_dataType" value="1"/>
//        <XMLAttribute output="no" name="RS_dataUsage" value="fact"/>
//    </XMLAttributes>
//</dataItem>